/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.podcast;

import com.bff.bjj.ui.component.MusicList;
import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddPodcastAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertPodcastAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadPodcastAction;
import com.bff.slimserver.musicjukebox.lib.actions.ViewPodcastAction;
import com.bff.bjj.utils.Utils;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.SlimPodcaster;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.SlimPodcastItem;

/**
 *
 * @author bfindeisen
 */
public class PodcastItemList extends MusicList implements MouseListener {

    private SlimPodcaster podcaster;
    private DefaultListModel podcastListModel;
    private SlimPlaylist playlist;
    private PodcastItemListCellRenderer podcastListCellRenderer;
    private List<SlimPodcastItem> podcastItemList;
    private JPopupMenu podcastMenu;

    public PodcastItemList() {
        try {
            playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(PodcastItemList.class.getName()).log(Level.SEVERE, null, ex);
        }
        podcastListModel = new DefaultListModel();
        try {
            setPodcaster(JukeboxProperties.getInstance().getPodcaster());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(PodcastItemList.class.getName()).log(Level.SEVERE, null, ex);
        }

        setPodcastListCellRenderer(new PodcastItemListCellRenderer());
        setCellRenderer(getPodcastListCellRenderer());

        setModel(podcastListModel);

        //listAll();
        addMouseListener(this);

    }

    public synchronized void setPodcastItems(Collection<SlimPodcastItem> podcastItems) {
        podcastListModel = new DefaultListModel();

        podcastItemList = new ArrayList<SlimPodcastItem>(podcastItems);

        for (SlimPodcastItem item : podcastItemList) {
            if (item.isAudio()) {
                podcastListModel.addElement(item);
            }
        }
        setModel(podcastListModel);
        setSelectedIndex(0);
    }

    public SlimPodcastItem getSelectedPodcast() {
        return (SlimPodcastItem) getSelectedValue();
    }

    public Collection<SlimPodcastItem> getSelectedPodcasts() {
        List<SlimPodcastItem> podcastItems = new ArrayList<SlimPodcastItem>();
        for (Object o : getSelectedValues()) {
            podcastItems.add((SlimPodcastItem) o);
        }

        return podcastItems;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the podcaster
     */
    public SlimPodcaster getPodcaster() {
        return podcaster;
    }

    /**
     * @param podcaster the podcaster to set
     */
    public void setPodcaster(SlimPodcaster podcaster) {
        this.podcaster = podcaster;
    }

    /**
     * @return the podcastListCellRenderer
     */
    public PodcastItemListCellRenderer getPodcastListCellRenderer() {
        return podcastListCellRenderer;
    }

    /**
     * @param podcastListCellRenderer the podcastListCellRenderer to set
     */
    public void setPodcastListCellRenderer(PodcastItemListCellRenderer podcastListCellRenderer) {
        this.podcastListCellRenderer = podcastListCellRenderer;
    }

    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            SlimPodcastItem item = getSelectedPodcast();

            //@TODO Podcast details
            //new AlbumThumbnailDialog(Utils.getParentComponent(this), song).setVisible(true);

        } else {
            createPodcastPlayPopUp(e);
        }
    }

    public void mousePressed(MouseEvent e) {
        createPodcastPlayPopUp(e);
    }

    public void mouseReleased(MouseEvent e) {
        createPodcastPlayPopUp(e);
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    private void createPodcastPlayPopUp(MouseEvent e) {
        if (e.isPopupTrigger()) {
            setSelectedIndex(locationToIndex(e.getPoint()));
            SlimPodcastItem podcast = getSelectedPodcast();
            podcastMenu = new JPopupMenu();
            podcastMenu.add(new JMenuItem(new PlaylistAddPodcastAction(getPlaylist(), podcast)));
            podcastMenu.add(new JMenuItem(new PlaylistInsertPodcastAction(getPlaylist(), podcast)));
            podcastMenu.add(new JMenuItem(new PlaylistLoadPodcastAction(getPlaylist(), podcast)));
            podcastMenu.add(new JMenuItem(new ViewPodcastAction(Utils.getParentComponent(this), podcast)));

            podcastMenu.setVisible(true);
            podcastMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }
}
